<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2019-12-10
 * Time: 14:16
 */
namespace app\api\controller\v1;
use app\api\controller\BaseController;
use app\api\model\MdbUser;
use app\api\model\UserInvitation;
use app\api\model\WechatUser;
use app\lib\exception\ParameterException;
use think\facade\Request;
use app\api\service\Token as TokenService;
use app\api\model\User as UserModel;

class User extends BaseController
{
    /**
     * 更新用户信息并返回用户最新信息
     * @http put
     * @url user
     */
    public function updateUser()
    {
        $uid = TokenService::getCurrentUid();
        $put = Request::put();
        $user = new UserModel();
        if(UserModel::get($uid)){
            $user->allowField(true)->save([
                'nickname' => $put['nickname'],
                'head_img' => $put['head_img'],
                'province' => $put['province'],
                'status' => 1,
                'city' => $put['city'],
                'gender' => $put['gender'],
                'user_no' => $this->getUserNo($uid)
            ],['id' => $uid]);
            return json(UserModel::get($uid),202);
        } else {
            throw new ParameterException([
                'msg' => [
                    'user' => '请重新更新用户信息'
                ]
            ]);
        }
    }

    // 获取会员编号
    private function getUserNo($uid)
    {
        $number = 6 - strlen($uid);
        if($number > 0) {
            return getRandNumber0($number).$uid;
        } else {
            return $uid;
        }
    }


    // 根据会员ID得到随机数
    private function getString($uid)
    {
        $number = 6 - strlen($uid);
        if($number > 0) {
            return getRandNumber($number).$uid;
        } else {
            return $uid;
        }
    }

    /**
     * 获取用户信息
     * @http get
     * @url user
     */
    public function getUserInfo()
    {
        $uid = TokenService::getCurrentUid();
        $info =  UserModel::get($uid);
        return $info;
    }


    public function getUserList()
    {
        $userList = UserModel::order('id desc')->select()->toArray();
        foreach ($userList as &$v){
            $userInfo = UserModel::where('openid',$v['openid'])->select()->toArray();
            if(count($userInfo) > 1) {
//                foreach ($userInfo as &$vo){
//                    UserModel::where('openid',$vo['id'])->delete();
//                }
//                if($userInfo['status'] == 0) {
//                    // UserModel::where('openid',$v['openid'])->delete();
//                }
                dump($userInfo);
            }
        }
    }
}